package li.com.springaidemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import li.com.springaidemo.common.R;
import li.com.springaidemo.pojo.Feedback;
import li.com.springaidemo.pojo.History;
import li.com.springaidemo.pojo.User;
import li.com.springaidemo.server.WebSocketServer;
import li.com.springaidemo.service.FeedbackService;
import li.com.springaidemo.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/feedback")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private WebSocketServer webSocketServer;

    @GetMapping("/page")
    public R getPage(@RequestParam(value = "input",defaultValue = "")String input, @RequestParam(value = "userId",defaultValue = "") Long userId, @RequestParam(value = "dispose",defaultValue = "") Long dispose){
        QueryWrapper<Feedback> queryWrapper=new QueryWrapper<>();
        if (!input.equals("")){
            queryWrapper.eq("id",input).or().eq("user_id",input).or().eq("user_name",input).or().eq("history_id",input).or().eq("history_name",input);
        }
        if (userId!=null&&userId!=1){
            queryWrapper.eq("user_id",userId);
        }
        if (dispose!=null){
            queryWrapper.eq("dispose",dispose);
        }
        queryWrapper.orderByAsc("dispose").orderByDesc("begin_time");
        List<Feedback> list = feedbackService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R addFeedback(@RequestBody Feedback feedback) {
        feedback.setBeginTime(LocalDateTime.now());
        feedback.setDispose("1");
        feedbackService.save(feedback);
        return R.success();
    }

//    将处理设置为'通过'
    @PostMapping("/pass")
    public R passFeedback(@RequestBody Feedback feedback) {
        feedback.setEndTime(LocalDateTime.now());
        feedback.setDispose("3");
        History history = historyService.getById(feedback.getHistoryId());
        history.setState(1);
        historyService.updateById(history);
        feedbackService.updateById(feedback);
        webSocketServer.sendToUserClient(feedback.getUserId()+"","处理已通过");
        return R.success();
    }
//    将处理设置为'未处理'
    @PostMapping("/overrule")
    public R overruleFeedback(@RequestBody Feedback feedback) {
        feedback.setDispose("1");
        feedbackService.updateById(feedback);
        webSocketServer.sendToUserClient(feedback.getUserId()+"","待用户处理");
        return R.success();
    }
//    将处理设置为'待审批'
    @PostMapping("/await")
    public R awaitFeedback(@RequestBody Feedback feedback) {
        feedback.setDispose("2");
        feedbackService.updateById(feedback);
        webSocketServer.sendToAdminClient("待管理员审批");
        return R.success();
    }

    @GetMapping("/delete")
    public R deleteFeedback(@RequestParam(value = "id") String id) {
        feedbackService.removeById(id);
        return R.success();
    }
}
